//
//  PKMusicPlayer.m
//  MusicDemo
//
//  Created by lanou on 15/12/24.
//  Copyright © 2015年 Huang. All rights reserved.
//

#import "PKMusicPlayer.h"
#import <AVFoundation/AVFoundation.h>


@interface PKMusicPlayer ()
@property (nonatomic, strong) AVPlayer *player;// 音乐播放器

@property (nonatomic, strong) NSTimer *timer;
@end

@implementation PKMusicPlayer

+ (instancetype)sharedPlayer {
    static PKMusicPlayer *player = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        player = [[PKMusicPlayer alloc] init];
    });
    return player;
}

- (instancetype)init {
    if ([super init]) {
        _player = [[AVPlayer alloc] init];
        
        
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
        
        _index = 0;
    }
    return self;
}

- (void)timerAction:(NSTimer *)timer {
    // 改变currentTime,外界访问直接调用get方法
    
    if (_player.rate == 1.0) {
        if (self.totalTime != 0 && self.currentTime == self.totalTime && self.isPlayMainControllerDealloc == YES) {
            [self nextMusic];
            NSLog(@"PKMusicPlayer-----下一首");
        }
    }
}


- (void)setMusicArray:(NSArray *)musicArray {
    _musicArray = musicArray;
    _userModel = _musicArray[_index];
    // 相当于给音乐播放器赋值的时候,同时会设置播放器默认播放的歌曲为第一首
    AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:_userModel.url]];
    [_player replaceCurrentItemWithPlayerItem:item];
}

- (void)beforeMusic {
    _index--;
    if (-1 == _index) {
        _index = _musicArray.count - 1;
    }
    if (_playType == RandomPlay) {
        _index = arc4random() % _musicArray.count;
    }
    [self changeMusicwithIndex:_index];
}

- (void)nextMusic {
    _index++;
    if (_musicArray.count == _index) {
        _index = 0;
    }
    // 随机播放的下一首
    if (_playType == RandomPlay) {
        _index = arc4random() % _musicArray.count;
    }
    [self changeMusicwithIndex:_index];
}

- (void)play {
    [self changeMusicwithIndex:_index];
}

- (void)pause {
    [_player pause];
}

- (void)seekToTime:(NSInteger)newTime {
    // value/timescale = seconds.
    // (可以看做)value : 总帧数   timescale : 每秒播放帧数
    // value = seconds * timescale;
    CMTime currentTime = _player.currentTime;
    currentTime.value = newTime * currentTime.timescale;
    [_player seekToTime:currentTime];
}

- (void)stop {
    [_player pause];
    [self seekToTime:0];
}

- (NSInteger)currentTime {
    if (_player.currentTime.timescale == 0) return 0;
    return _player.currentTime.value / _player.currentTime.timescale;
}

- (NSInteger)totalTime {
    CMTime time = _player.currentItem.duration;
    if (time.timescale == 0) return 0;
    return time.value / time.timescale;
}

- (BOOL)isPlaying {// 播放状态判断
    if (_player.rate == 1.0) {
        return YES;
    } else {
        return NO;
    }
}

- (void)finishPlay {
    if (_playType == SinglePlay) {
        [self seekToTime:0];// 重新播放
    } else {
        [self nextMusic];// 下一首
    }
}


- (void)changeMusicwithIndex:(NSInteger)index {
    _index = index;
    if (_musicArray.count == 0) {
        [_player play];
    }
    else
    {
        if (-1 == _index) {
            _index = _musicArray.count - 1;
        }
        if (_musicArray.count == _index) {
            _index = 0;
        }
        // 随机播放的下一首
        if (_playType == RandomPlay) {
            _index = arc4random() % _musicArray.count;
        }
        _userModel = _musicArray[_index];
        NSURL *url = [NSURL URLWithString:_userModel.url];
        AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:url];
        [_player replaceCurrentItemWithPlayerItem:item];
        [_player play];
    }
//    self.block();
}

@end
